package carpediem.xin.tamplate.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.util.DisplayMetrics;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import carpediem.xin.tamplate.R;

public class MainActivity extends BaseActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @BindView(R.id.imageView)
    protected ImageView vImage;
    @BindView(R.id.button)
    protected Button vChangeImage;

    protected int curImageUrlIndex = 0;
    protected String[] ImageUrls = {
            "https://img1.qunarzz.com/travel/poi/1412/5e/5e4f912053dcd444cdb.jpg_r_640x640_869f31eb.jpg",
            "https://img1.qunarzz.com/travel/d6/1703/d4/cc2be57b6592e0b5.jpg",
            "https://img1.qunarzz.com/travel/d7/1703/66/c7a36a01a8e2ab5.jpg_r_1024x683x95_4ed46899.jpg",
            "https://img1.qunarzz.com/travel/poi/201211/15/e33df3954b5b6dc7ddb12cfb.jpg_r_1024x683x95_b4e0e6bd.jpg"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        loadImage(ImageUrls[curImageUrlIndex], vImage);
    }

    private void loadImage(String imageUrl, ImageView vImage) {
        Glide.with(this).load(imageUrl).asBitmap().into(new BitmapImageViewTarget(vImage) {
            @Override
            protected void setResource(Bitmap resource) {
                super.setResource(resource);
                curImageUrlIndex ++;
                int buttonWidth = vChangeImage.getWidth();
                int buttonHeight = vChangeImage.getHeight();
                new Palette.Builder(resource)
                        .setRegion(resource.getWidth() - buttonWidth, resource.getHeight() - buttonHeight, resource.getWidth(), resource.getHeight())
                        .generate(new Palette.PaletteAsyncListener() {
                            @Override
                            public void onGenerated(Palette palette) {
                                Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
                                if (vibrantSwatch != null) {
                                    vChangeImage.setTextColor(vibrantSwatch.getTitleTextColor());
//                                    vChangeImage.setBackgroundColor(vibrantSwatch.getRgb());
                                }
                            }
                        });
            }
        });
    }

    @OnClick(R.id.button)
    public void changeImage() {
        if (curImageUrlIndex == ImageUrls.length - 1) {
            curImageUrlIndex = 0;
        }
        loadImage(ImageUrls[curImageUrlIndex], vImage);
    }
}
